package com.jprocms.module.cms.config;

import com.jprocms.module.cms.framework.util.SpringUtil;
import com.jprocms.module.cms.inteceptor.SiteContextInteceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import static com.jprocms.module.cms.constant.InterceptorOrder.SITE_INTERCEPTOR_ORDER;

/**
 * @author jprocms
 */
@Configuration(proxyBeanMethods = false)
public class WebMvcConfig implements WebMvcConfigurer {

    private final SiteContextInteceptor siteContextInteceptor;
    private TplConfigProperties tplConfigProperties;

    private JproProperties jproProperties;

    public WebMvcConfig(SiteContextInteceptor siteContextInteceptor, TplConfigProperties tplConfigProperties,JproProperties jproProperties) {
        this.siteContextInteceptor = siteContextInteceptor;
        this.tplConfigProperties = tplConfigProperties;
        this.jproProperties = jproProperties;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(siteContextInteceptor).addPathPatterns("/**")
                .excludePathPatterns("/doc.html", "/swagger-ui.html", "/v3/api-docs",
                        "/swagger-resources/**", "/webjars/**", "/u/**", "/res/**" , "/rpc-api/cms/site/create", "/rpc-api/cms/global-config/get").order(SITE_INTERCEPTOR_ORDER);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //本地上传资源映射,和文件配置的基础路径相同
        registry.addResourceHandler("/upload/**")
                .addResourceLocations("file:" + jproProperties.getUploadPath()+"/upload");

        registry.addResourceHandler("/res/**")
                .addResourceLocations(tplConfigProperties.getTplLocalConfig().getResPath());
    }


}
